<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="300px">
    <el-form
      :model="formData"
      ref="formRef"
      :rules="formRules"
      v-loading="formLoading"
      label-width="auto"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="标签名称" prop="name">
            <el-input v-model="formData.name" placeholder="请输入标签名称" :clearable="true" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="颜色" prop="color">
            <el-color-picker
              v-model="formData.color"
              :predefine="predefineColors"
              @active-change="handleBlurColor"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button type="primary" :disabled="formLoading" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup name="MailLabelEdit" lang="ts">
import * as MailApi from '@/api/mailbox/mail'
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
let formType = ''
const dialogTitle = ref('')
const dialogVisible = ref(false)
const formRef = ref()
const formLoading = ref(false)
const formData = ref({
  name: '',
  color: '',
  ownerId: 0
})
const predefineColors = ref([
  '#ff8080', // Light Pinkish Red
  '#ffff80', // Pale Yellow
  '#80ff80', // Light Green
  '#00ff80', // Bright Green
  '#80ffff', // Light Cyan
  '#0080ff', // Bright Blue
  '#ff80c0', // Light Pinkish Purple
  '#ff80ff', // Bright Pinkish Purple

  '#ff0000', // Red
  '#ffff00', // Yellow
  '#80ff00', // Bright Green
  '#00ff40', // Brighter Green
  '#00ffff', // Cyan
  '#0080c0', // Light Blue
  '#8080c0', // Light Purple
  '#ff00ff', // Magenta

  '#804040', // Dark Pinkish Red
  '#ff8040', // Orange
  '#00ff00', // Bright Lime Green
  '#008080', // Teal
  '#004080', // Dark Blue
  '#8080ff', // Bright Purple
  '#800040', // Dark Pinkish Purple
  '#ff0080', // Bright Pink

  '#800000', // Maroon
  '#ff8000', // Orange
  '#008000', // Green
  '#008040', // Darker Green
  '#0000ff', // Blue
  '#0000a0', // Dark Blue
  '#800080', // Purple
  '#8000ff', // Violet

  '#400000', // Very Dark Red
  '#804000', // Dark Orange
  '#004000', // Dark Green
  '#004040', // Dark Teal
  '#000080', // Navy Blue
  '#000040', // Very Dark Blue
  '#400040', // Dark Purple
  '#400080', // Darker Purple

  '#000000', // Black
  '#808000', // Olive
  '#808040', // Dark Olive
  '#808080', // Gray
  '#408080', // Dark Teal
  '#c0c0c0' // Light Gray
])
const formRules = reactive({
  name: [{ required: true, message: '标签名称不能为空', trigger: 'change' }]
})
/** 打开弹窗 */
const open = async (type: string, id?: number, ownerUserId) => {
  dialogVisible.value = true
  formData.value.ownerId = ownerUserId
  dialogTitle.value = t('action.' + type)
  formType = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await MailApi.singleGetLabel(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    name: '',
    color: '',
    ownerId: 0
  }
  formRef.value?.resetFields()
}

const emit = defineEmits(['success'])
/** 提交表单 */
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    if (formType === 'create') {
      await MailApi.createLabel(formData.value)
      message.success(t('common.createSuccess'))
    } else {
      await MailApi.updateLabel(formData.value)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
const handleBlurColor = (data) => {
  formData.value.color = data
}
</script>
<style scoped>
:deep(.el-color-picker__trigger) {
  width: 189px;
}
:deep(.el-color-picker__color-inner) {
  align-items: center;
  display: inline-flex;
  height: 100%;
  justify-content: flex-end;
  width: 100%;
  padding-right: 10px;
}
</style>
